﻿/*
* Copyright 2010 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ngilead.Test.DAO;
using Ngilead.Test.Models;
using NHibernate.Criterion;
using System.ServiceModel;
using Ngilead.Test.Models.Stateless;
using System.Runtime.Serialization;
using Iesi.Collections.Generic;

namespace Ngilead.Test.Services
{
    /// <summary>
    /// User service class
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class UserService : IUserService
    {
        #region DAO

        private IUserDAO _userDAO;

        /// <summary>
        /// User DAO
        /// </summary>
        private IUserDAO UserDAO
        {
            get
            {
                if (_userDAO == null)
                    _userDAO = DAOFactory.UserDAO;
                return _userDAO;
            }
        }

        #endregion

        #region Generic

        /// <summary>
        /// Get user by id
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns>User</returns>
        public User LoadById(Int64 id)
        {
            return (User)UserDAO.LoadById(id).Clone();
        }

        /// <summary>
        /// Load all users by id
        /// </summary>
        /// <returns>All users</returns>
        public IList<User> LoadAll()
        {
            return (IList<User>)UserDAO.LoadAll().Clone();
        }

        /// <summary>
        /// Count all users
        /// </summary>
        /// <returns>Count</returns>
        public Int64 CountAll()
        {
            return UserDAO.CountAll();
        }

        /// <summary>
        /// Save or update user
        /// </summary>
        /// <param name="obj">User</param>
        public void Save(User obj)
        {
            obj = (User)obj.Merge();
            UserDAO.Save(obj);
        }

        /// <summary>
        /// Delete user
        /// </summary>
        /// <param name="obj">User</param>
        public void Delete(User obj)
        {
            obj = (User)obj.Merge();
            UserDAO.Delete(obj);
        }

        #endregion

        /// <summary>
        /// Load user by login
        /// </summary>
        /// <param name="login">Login</param>
        /// <returns>User</returns>
        public User LoadUserByLogin(String login)
        {
            return (User)UserDAO.LoadUserByLogin(login).Clone();
        }

        /// <summary>
        /// Load user and groups by login
        /// </summary>
        /// <param name="login">Login</param>
        /// <returns>User</returns>
        public User SearchUserAndGroupsByLogin(String login)
        {
            return (User)UserDAO.SearchUserAndGroupsByLogin(login).Clone();
        }

        /// <summary>
        /// Load user and message by login
        /// </summary>
        /// <param name="login">Login</param>
        /// <returns>User</returns>
        public User SearchUserAndMessagesByLogin(String login)
        {
            return (User)UserDAO.SearchUserAndMessagesByLogin(login).Clone();
        }

        /// <summary>
        /// Load user and group and message by login
        /// </summary>
        /// <param name="login">Login</param>
        /// <returns>User</returns>
        public User SearchUserAndGroupAndMessagesByLogin(String login)
        {
            return (User)UserDAO.SearchUserAndGroupAndMessagesByLogin(login).Clone();
        }

        /// <summary>
        /// Load all users and messages
        /// </summary>
        /// <returns>Users</returns>
        public IList<User> LoadAllUserAndMessages()
        {
            return (IList<User>)UserDAO.LoadAllUserAndMessages().Clone();
        }
    }
}
